package com.atguigu.sort;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

//快速排序
public class QuickSort {
    public static void main(String[] args) {
        int arr[] = new int[8000001];
        for (int i=0;i<8000001;i++){
            arr[i] = (int) (Math.random() * 8000000);
        }
        Date date1 = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY/MM/dd HH:mm:ss.SSS");
        String format = simpleDateFormat.format(date1);
        System.out.println("遍历前的时间"+format);
        quickSort(arr,0,8000000);
        Date date2 = new Date();
        String format1 = simpleDateFormat.format(date2);
        System.out.println("遍历后的时间"+format1);
    }

    public static void quickSort(int arr[],int left,int right){
        int l = left;
        int r = right;
        if (l>=r){
            return;
        }

        int temp = sort(arr, l, r);
        quickSort(arr,l,temp-1);
        quickSort(arr,temp+1,right);
    }

    public static int sort(int arr[], int left, int right) {
        int l = left;
        int r = right;
        //坑位的值
        int zj = arr[(left + right) / 2];
        //坑位所在的位置
        int temp = (left + right) / 2;



        while (l < r) {
            while (l < r) {
                if (arr[l] > zj) {
                    arr[temp] = arr[l];
                    temp = l;
                    break;
                }
                if (l==temp){
                    break;
                }
                l++;
            }

            while (r > l) {
                if (arr[r] < zj) {
                    arr[temp] = arr[r];
                    temp = r;
                    break;
                }

                if (r==temp){
                    break;
                }
                r--;
            }
        }
        arr[temp] = zj;
        return temp;
    }

}
